%% (c) Copyright Bearice 2010.  All rights reserved. 
%% See COPYING for more copyrights details
%% TODO: Add description to minitrue

-module(minitrue).
-include_lib("yaws/include/yaws.hrl").
-include_lib("miniplenty/include/log.hrl").
-behaviour(application).
-behaviour(supervisor).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------
-include("rpc.hrl").
%% --------------------------------------------------------------------
%% Behavioural exports
%% --------------------------------------------------------------------
-export([
	 start/2,
	 stop/1,
	 init/1
        ]).

%% --------------------------------------------------------------------
%% Internal exports
%% --------------------------------------------------------------------
-export([]).

%% --------------------------------------------------------------------
%% Macros
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% Records
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% API Functions
%% --------------------------------------------------------------------


%% ====================================================================!
%% External functions
%% ====================================================================!
%% --------------------------------------------------------------------
%% Func: start/2
%% Returns: {ok, Pid}        |
%%          {ok, Pid, State} |
%%          {error, Reason}
%% --------------------------------------------------------------------
start(_Type, StartArgs) ->
	?INFO("config yaws..."),
    GC = (yaws_config:make_default_gconf(false, "app1984"))
		 #gconf{logdir="logs",
				yaws="RPC SERVER"},
    SC = #sconf{port = 8080,
                listen = {0,0,0,0},
                docroot = "htdocs",
				allowed_scripts = [yaws],
				appmods = [
					{"/rpc",minitrue_rpc_handler}
				]},
    yaws_api:setconf(GC, [[SC]]),
    case supervisor:start_link({local,?MODULE},?MODULE,StartArgs) of
	{ok, Pid} ->
	    {ok, Pid};
	Error ->
	    Error
    end.

%% --------------------------------------------------------------------
%% Func: stop/1
%% Returns: any
%% --------------------------------------------------------------------
stop(_State) ->
    ok.

%% --------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok,  {SupFlags,  [ChildSpec]}} |
%%          ignore                          |
%%          {error, Reason}
%% --------------------------------------------------------------------
init(_Args) ->
	Table = ets:new(minitrue_rpc_service,[
		set,
		{keypos,2},
		public
	]),
	{ok,{{one_for_one,60,60},[
		{minitrue_rpc_registry,{minitrue_rpc_registry,start_link,[Table]},
		 	permanent,2000,worker,[minitrue_rpc_registry]},
		{example,{example,start_link,[]},
		 	permanent,2000,worker,[example]}		  
	]}}.
%% ====================================================================
%% Internal functions
%% ====================================================================

